package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.coolapp.service.CoolAppCardService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.coolapp.pojo.dto.CoolAppCloseCardDTO;
import com.xbongbong.pro.coolapp.pojo.dto.CoolAppSendCardDTO;
import com.xbongbong.pro.coolapp.pojo.dto.CoolAppSendRemindTopCardDTO;
import com.xbongbong.pro.coolapp.pojo.dto.CoolAppWorkReportTopCardDTO;
import com.xbongbong.pro.coolapp.pojo.dto.CoolAppUpdateCardDTO;
import com.xbongbong.pro.coolapp.pojo.dto.CoolAppWorkflowCardDTO;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zhushuainan
 * @title: CoolAppCardController
 * @projectName xbb-pro
 * @description: 酷应用卡片接口
 * @date 2022/6/13 10:27
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/coolapp/card")
public class CoolAppCardController {

    @Resource
    private CoolAppCardService coolAppCardService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    @RequestMapping(value = "/send", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendCoolAppCard(@RequestBody @Valid CoolAppSendCardDTO coolAppSendCardDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(coolAppSendCardDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(coolAppSendCardDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                TransmittableThreadLocalUtil.setValue(coolAppSendCardDTO.getSaasMark(), coolAppSendCardDTO.getBusinessType(), coolAppSendCardDTO.getMenuId(), DistributorMarkEnum.OTHER.getCode());
                BaseVO baseVO = coolAppCardService.sendCard(coolAppSendCardDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateCoolAppCard(@RequestBody @Valid CoolAppUpdateCardDTO coolAppUpdateCardDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(coolAppUpdateCardDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(coolAppUpdateCardDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                TransmittableThreadLocalUtil.setValue(coolAppUpdateCardDTO.getSaasMark(), coolAppUpdateCardDTO.getBusinessType(), coolAppUpdateCardDTO.getMenuId(), DistributorMarkEnum.OTHER.getCode());
                BaseVO baseVO = coolAppCardService.updateCard(coolAppUpdateCardDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/send/workflow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendWorkflowCard(@RequestBody @Valid CoolAppWorkflowCardDTO coolAppWorkflowCardDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(coolAppWorkflowCardDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(coolAppWorkflowCardDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                TransmittableThreadLocalUtil.setValue(coolAppWorkflowCardDTO.getSaasMark(), coolAppWorkflowCardDTO.getBusinessType(), coolAppWorkflowCardDTO.getMenuId(), DistributorMarkEnum.OTHER.getCode());
                BaseVO baseVO = coolAppCardService.sendWorkflowCard(coolAppWorkflowCardDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/update/workflow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateWorkflowCard(@RequestBody @Valid CoolAppWorkflowCardDTO coolAppWorkflowCardDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(coolAppWorkflowCardDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(coolAppWorkflowCardDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                TransmittableThreadLocalUtil.setValue(coolAppWorkflowCardDTO.getSaasMark(), coolAppWorkflowCardDTO.getBusinessType(), coolAppWorkflowCardDTO.getMenuId(), DistributorMarkEnum.OTHER.getCode());
                BaseVO baseVO = coolAppCardService.updateWorkflowCard(coolAppWorkflowCardDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/send/remindTopCard", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendCommunicateTopCard(@RequestBody @Valid CoolAppSendRemindTopCardDTO coolAppSendRemindTopCardDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(coolAppSendRemindTopCardDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(coolAppSendRemindTopCardDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                BaseVO baseVO = coolAppCardService.sendCommunicateTopCard(coolAppSendRemindTopCardDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/closeTopCard", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String closeTopCard(@RequestBody @Valid CoolAppCloseCardDTO coolAppCloseCardDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(coolAppCloseCardDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(coolAppCloseCardDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                BaseVO baseVO = coolAppCardService.closeTopCard(coolAppCloseCardDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/send/communicatePlanTopCard", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendCommunicatePlanTopCard(@RequestBody CoolAppSendRemindTopCardDTO coolAppSendRemindTopCardDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(coolAppSendRemindTopCardDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(coolAppSendRemindTopCardDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                BaseVO baseVO = coolAppCardService.sendCommunicatePlanTopCard(coolAppSendRemindTopCardDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/send/WorkReportTopCard", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendWorkReportTopCard(@RequestBody CoolAppWorkReportTopCardDTO coolAppWorkReportTopCardDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(coolAppWorkReportTopCardDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(coolAppWorkReportTopCardDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                BaseVO baseVO = coolAppCardService.sendWorkReportTopCard(coolAppWorkReportTopCardDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
        * @description: TODO
        * @param menuInitDTO
         * @param br
        * @return java.lang.String
        * @author zhushuainan
        * @date 2022/08/11 09:36:52
        */
    @RequestMapping(value = "/init", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initCoolAppBundle(@RequestBody @Valid MenuInitDTO menuInitDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(menuInitDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(menuInitDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                BaseVO baseVO = coolAppCardService.initCoolAppBundle(menuInitDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
